/*
    Copyright 2010 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.util;

import javax.swing.*;
import java.awt.*;
import java.io.*;

/**
 *
 * @author Barnet Wagman
 */
public class GuiUtil {
    
    public static File CURRENT_DIR = new File(System.getProperty("user.dir"));
    
    public GuiUtil() {}
    
    public static File getFile(String title) {
        return(getFile(title,CURRENT_DIR));
    }
    
    public static File getFile(String title,File initialDir) {        
        JFileChooser fChooser = new JFileChooser(initialDir);     
        fChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fChooser.setDialogTitle(title);
        if ( fChooser.showOpenDialog(null) != 
            JFileChooser.APPROVE_OPTION ) return(null);
        File c = fChooser.getSelectedFile();
        CURRENT_DIR = c.getParentFile();
        return(c);
    }   
    
    public static File saveFile(String title,File initialDir,File defaultFile) {        
        JFileChooser fChooser = new JFileChooser(initialDir);     
        fChooser.setSelectedFile(defaultFile);
        fChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
       
        fChooser.setDialogTitle(title);
        int r = fChooser.showSaveDialog(null);
        if  ( r != JFileChooser.APPROVE_OPTION ) return(null);   
        File c = fChooser.getSelectedFile();
        CURRENT_DIR = c.getParentFile();
        return(c);
    }   
    
    public static File getDir(String title) {
         return(getDir(title,CURRENT_DIR));
    }
    
    public static File getDir(String title,File initialDir) {                
        JFileChooser fChooser = new JFileChooser(initialDir);     
        fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fChooser.setDialogTitle(title);
        if ( fChooser.showOpenDialog(null) != 
            JFileChooser.APPROVE_OPTION ) return(null);
        File c = fChooser.getSelectedFile();
        CURRENT_DIR = c;
        return(c);
    }   
    
    public static String promptForString(String message,String initialValue) {
        return( JOptionPane.showInputDialog(message,initialValue) );        
    }
    
        /** A <tt>JOptionPane</tt> confirmation dialog.
         */
    public static boolean answerIsYes(String[] mess,String title) {
        int r = JOptionPane.showConfirmDialog(null, mess, title, 
				   JOptionPane.YES_NO_OPTION);

	return(r == JOptionPane.YES_OPTION);    
    }
    
    public static void notice(Object mess, String title) {
        JOptionPane.showMessageDialog(null,mess,title,
                                      JOptionPane.INFORMATION_MESSAGE);
    }
    
    public static String getChoice(String[] choices) {
        String selectedValue = (String)
            JOptionPane.showInputDialog(null,
                                        "Choose", "",
                                        JOptionPane.INFORMATION_MESSAGE, null,
                                        choices, choices[0]);
        return(selectedValue);
    }
    
             // ---------- Screen size methods -----------------------
    
    public static JLabel LABEL_SAMPLE = new JLabel("Abc");
    
    
    public static int getScreenWidth(JComponent c) {
	return( getScreenSize(c).width );
    }

    public static int getScreenWidth() {
	return( getScreenWidth(LABEL_SAMPLE) );
    }

    public static int getScreenHeight(JComponent c) {
	return( getScreenSize(c).height );
    }

    public static int getScreenHeight() {
	return( getScreenHeight(LABEL_SAMPLE) );
    }

    public static Dimension getScreenSize(JComponent c) {
        return(c.getToolkit().getScreenSize());
    }
    
    public static Dimension getScreenSize() {
        return(getScreenSize(LABEL_SAMPLE));
    }

        // ----------------------------------------

    public static void centerOnScreen(Container c) {

	Dimension d = c.getPreferredSize();
	int center_x = roundMult(getScreenWidth(),0.5f);
	int center_y = roundMult(getScreenHeight(),0.5f);
	int dx = roundMult(d.width,0.5f);
	int dy = roundMult(d.height,0.5f);
	c.setLocation(center_x - dx, center_y- dy);
    }

    public static int roundMult(int v,float factor) {
	return((int) Math.round(((float) v) * factor));
    }

        // ------------ Decoration ---------------------

    public static void setNBLIcon(JFrame f) {
        try {
            java.awt.image.BufferedImage img =
                javax.imageio.ImageIO.read((new Object()).getClass().getResource(
                                    "/com/norbl/util/graphics/nb_icon_sharp_35.jpeg"));
            f.setIconImage(img);
        }
        catch(Exception x) { throw new RuntimeException(x); }
    }
}
